class UaliaPhotosController < ApplicationController
layout "page"
  def index
    @ualia = Ualia.find(params[:ualia_id])
    @photo_pages = Paginator.new(self, @ualia.photos.count, 12, params[:page])
    @photos = @ualia.photos.find(:all, :order => 'created_at DESC',
                                :limit => @photo_pages.items_per_page,
                                :offset => @photo_pages.current.offset)
    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @photos.to_xml }
    end
  end
  
  def new
 #  @article_id = params[:article_id]
    @photo = Photo.new
  end
  
  def create
    @photo = Photo.new(params[:photo])

    respond_to do |format|      
    if Ualia.find(params[:ualia_id]).photos << @photo
        flash[:notice] = 'Photo was successfully created.'
        format.html { redirect_to :controller=>'ualia_photos',:action=>'index',:ualia_id=> params[:ualia_id] }
        format.xml  { head :created, 
          :location => { redirect_to :controller=>'ualia_photos',:action=>'show',:ualia_id => @photo.ualia_id, :id => @photo}}
      else
        format.html { render :action => 'new' }
        format.xml  { render :xml => @photo.errors.to_xml }
      end
    end    
  rescue ActiveRecord::RecordInvalid
    render :action => 'new'
  end
  
  def show
    @photo = Photo.find_by_ualia_id_and_id(params[:ualia_id], 
                                          params[:id], 
                                          :include => :ualia)

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @photo.to_xml }
    end
  end
  

  
  def edit
    @photo = Ualia.find(params[:ualia_id]).photos.find(params[:id])    
  rescue ActiveRecord::RecordNotFound
    redirect_to :action => 'index'    
  end
  
  def update
    @photo = Ualia.find(params[:ualia_id]).photos.find(params[:id])

    respond_to do |format|
      if @photo.update_attributes(params[:photo])
        flash[:notice] = 'Photo was successfully updated.'
        format.html { redirect_to :controller=>'ualia_photos',:action=>'show',:ualia_id=>Ualia.find(params[:ualia_id]), :id=>@photo }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @photo.errors.to_xml }
      end
    end
  rescue ActiveRecord::RecordNotFound
    redirect_to :action => 'index'  
  end

  def destroy
      @photo = Ualia.find(params[:ualia_id]).photos.find(params[:id])
    @photo.destroy

    respond_to do |format|
      format.html { redirect_to :controller=>'ualia_photos',:action=>'index' }
      format.xml  { head :ok }
    end
  rescue ActiveRecord::RecordNotFound
    redirect_to :action => 'index'    
  end


  
end
